<?php
require_once 'application/core/Singleton.class.php';
class BaseModel
{   
    protected $table = '';
    protected $feild = '*';
    protected $where = '';
    protected $group = '';
    protected $having = '';
    protected $order = '';
    protected $limit = '';
    public function __call($function_name,$arr){
        $this->$function_name = $arr[0];
        return $this;
    }
    public function insert($arr){
        $mysql_ob = Singleton::getInstance('news');
        $re = $mysql_ob->table($this->table)->values($arr)->insert();
        return $re;
    }
    public function delete($where=null){
        $mysql_ob = Singleton::getInstance('news');
        if(!empty($where)){
            $re = $mysql_ob->table($this->table)->where($where)->delete();
        }else {
            if(!empty($this->where)){
                $re = $mysql_ob->table($this->table)->where($this->where)->delete();
            }else {
                $re = $mysql_ob->table($this->table)->delete();
            }
        }
        $this->init();
        return $re;
    }
    public function update($arr,$where = null){
        $mysql_ob = Singleton::getInstance('news');
        if(!empty($where)){
            $re = $mysql_ob->table($this->table)->set($arr)->where($where)->update();
        }else{
            if(!empty($this->where)){
                $re = $mysql_ob->table($this->table)->set($arr)->where($this->where)->update();
            }else{
                $re = $mysql_ob->table($this->table)->set($arr)->update();
            }
        }
        $this->init();
        return $re;
    }
    public function select($debug = false){
        if(empty($this->table)){
            return false;
        }
        if(empty($this->feild)){
            $this->feild = '*';
        }
        if(!empty($this->where)){
            $where = ' WHERE '.$this->where;
        }else{
            $where = '';
        }
        if(!empty($this->group)){
            $group = ' GROUP BY '.$this->group;
        }else{
            $group = '';
        }
        if(!empty($this->having)){
            $having = ' HAVING '.$this->having;
        }else{
            $having = '';
        }
        if(!empty($this->order)){
            $order = 'ORDER BY '.$this->order;
        }else{
            $order = '';
        }
        if(!empty($this->limit)){
            $limit = ' LIMIT '.$this->limit;
        }else{
            $limit = '';
        }
        $sql = "SELECT {$this->feild} FROM {$this->table} {$where} {$group} {$having} {$order} {$limit}";
        $mysql_ob = Singleton::getInstance('news');
        $this->init();//重置数据
        if($debug){
            var_dump($sql);exit();
        }else{
            $re = $mysql_ob->query($sql);
            return $re;
        }
    }
    
    public function init(){
        $this->feild = '*';
        $this->where = '';
        $this->group = '';
        $this->having = '';
        $this->order = '';
        $this->limit = '';
    }
    public function query($sql){
        $mysql_ob = Singleton::getInstance('news');
        $re = $mysql_ob->query($sql);
        return $re;
    }
}